package com.android.baseconfig.common.utils;
/*
 * -----------------------------------------------------------------
 * Copyright © 2016年 clife. All rights reserved.
 * Shenzhen H&T Intelligent Control Co.,Ltd.
 * -----------------------------------------------------------------
 * File: TUtil.java
 * Create: 16/6/20 22:35
 * Author: uuxia-mac
 */

import android.app.Activity;
import android.util.Log;

import com.android.baseconfig.base.BaseRetrofit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class TUtil {

    public static <T> Class<?> getTT(Object o) {
        try {
            Class clazz = o.getClass();
            Type t2 = clazz.getGenericSuperclass();
            Type[] ttt = ((ParameterizedType) t2).getActualTypeArguments();
            String name = ((Class<T>) ttt[0]).getName();
            Class<?> cc = Class.forName(name);
            Log.d("", cc.toString());
            return cc;
//            return (T) ttt[0];
//            return ((Class<T>) ttt[0]).newInstance();
//            return ((Class<T>) ((ParameterizedType) (o.getClass().getGenericSuperclass())).getActualTypeArguments()[i]).newInstance();
        } catch (ClassCastException e) {
//            e.printStackTrace();
        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
        }
        return null;
    }

    public static void getTTK(Object o) {
        try {
            Class clazz = o.getClass();
            Type t2 = clazz.getGenericSuperclass();
            Type tttt = ((ParameterizedType) t2).getRawType();
            String name = ((Class<?>) tttt).getName();
            Class<?> clzz = Class.forName(name);
//            Method setMethod = cls.getDeclaredMethod("setAct",Activity.class);
//            setMethod.invoke(cls.newInstance(), o);
            Method m2 = clzz.getDeclaredMethod("setContext", Activity.class);
            m2.invoke(clzz.newInstance(), o);
            Log.d("","sssssss");
        } catch (ClassCastException e) {
//            e.printStackTrace();
        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
        } catch (IllegalAccessException e) {
//            e.printStackTrace();
        } catch (InvocationTargetException e) {
//            e.printStackTrace();
        } catch (InstantiationException e) {
//            e.printStackTrace();
        }
    }

    public static <T> T getT(Object o, int i) {
        try {
            return ((Class<T>) ((ParameterizedType) (o.getClass().getGenericSuperclass())).getActualTypeArguments()[i]).newInstance();
        } catch (InstantiationException e) {
//            e.printStackTrace();
        } catch (IllegalAccessException e) {
//            e.printStackTrace();
        } catch (ClassCastException e) {
//            e.printStackTrace();
        }
        return null;
    }

    public static <T> T getTM(Object o, int i) {
        try {
            Type obj = o.getClass().getGenericSuperclass();
            Class<T> type = ((Class<T>) ((ParameterizedType) (obj)).getActualTypeArguments()[i]);
            T base = type.newInstance();
            if (base instanceof BaseRetrofit) {
                BaseRetrofit a = (BaseRetrofit) base;
                if (o instanceof Activity) {
                    a.setContext((Activity) o);
                }
            }
            return base;
        } catch (InstantiationException e) {
//            e.printStackTrace();
        } catch (IllegalAccessException e) {
//            e.printStackTrace();
        } catch (ClassCastException e) {
//            e.printStackTrace();
        }
        return null;
    }

    public static void setActivity(Object o, int i) {
        try {
            Type obj = o.getClass().getGenericSuperclass();
            Class<?> type = ((Class<?>) ((ParameterizedType) (obj)).getActualTypeArguments()[i]);
            Object base = type.newInstance();
            if (base instanceof BaseRetrofit) {
                BaseRetrofit a = (BaseRetrofit) base;
                if (o instanceof Activity) {
                    a.setContext((Activity) o);
                }
            }
        } catch (InstantiationException e) {
//            e.printStackTrace();
        } catch (IllegalAccessException e) {
//            e.printStackTrace();
        } catch (ClassCastException e) {
//            e.printStackTrace();
        }
    }


    public static Class<?> forName(String className) {
        try {
            return Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
